package com.his.outpatientvisit.service.impl;

import com.his.dictitem.entity.DictItem;
import com.his.dictitem.entity.DictItem_Detailed;
import com.his.dictitem.service.DictItemServcie;
import com.his.outpatientvisit.dao.PatiOutVisitSettleItemMapper;
import com.his.outpatientvisit.entity.PatiOutVisitSettleItem;
import com.his.outpatientvisit.service.PatiOutVisitSettleItemService;
import com.his.outpatientvisit.vo.PatiOutVisitSettleItemVo;
import com.xiao.common.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class PatiOutVisitSettleItemServiceImpl implements PatiOutVisitSettleItemService {

    @Resource
    private PatiOutVisitSettleItemMapper patiOutVisitSettleItemMapper;

    @Autowired
    private DictItemServcie dictItemServcie;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertPatiOutVisitSettleItem(List<PatiOutVisitSettleItemVo> patiOutVisitSettleItemVoList, String settleId) {
        for(PatiOutVisitSettleItemVo patiOutVisitSettleItemVo:patiOutVisitSettleItemVoList){
            PatiOutVisitSettleItem patiOutVisitSettleItem = BeanUtil.copyBean(patiOutVisitSettleItemVo,PatiOutVisitSettleItem.class);
            DictItem_Detailed item_detailed = dictItemServcie.queryDetailedItem(patiOutVisitSettleItem.getItemid());
            patiOutVisitSettleItem.setSettleid(settleId);
            patiOutVisitSettleItem.setQuantity(new BigDecimal(1));
//            patiOutVisitSettleItem.setPackageunit(item_detailed.get);
            patiOutVisitSettleItemMapper.povsitem_add(patiOutVisitSettleItem);
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertPatiOutVisitSettleItemOut(PatiOutVisitSettleItemVo patiOutVisitSettleItemVo,String settleId) {
        PatiOutVisitSettleItem povsi = patiOutVisitSettleItemMapper.povsitem_select(patiOutVisitSettleItemVo.getSettleid());
        povsi.setSettleid(settleId);
        povsi.setQuantity(povsi.getQuantity().multiply(new BigDecimal(-1)));
        povsi.setSalesprice(povsi.getSalesprice().multiply(new BigDecimal(-1)));
        patiOutVisitSettleItemMapper.povsitem_add(povsi);
    }

}
